home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Tennis / StringMatchPredictor.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-23  |  3.4 KB  |  97 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    StringMatchPredictor.h
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // An efficient O(N) string-matching predictor (where N is the size of the window).
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #ifndef __STRINGMATCHPREDICTOR_H
  14. #define __STRINGMATCHPREDICTOR_H
  15.  
  16. //----------------------------------------------------------------------------------------------
  17. // Include files
  18. //----------------------------------------------------------------------------------------------
  19.  
  20. #include "slidingwindow.h"
  21. #include "iteratorlist.h"
  22. #include "array.h"
  23. #include "predictor.h"
  24.  
  25. //----------------------------------------------------------------------------------------------
  26. // Defines
  27. //----------------------------------------------------------------------------------------------
  28.  
  29. #define SMP_HISTOGRAM CArray< CIteratorList< THistogramData > >
  30. #define SMP_WINDOW CSlidingWindow <TSequenceData>
  31.  
  32. //----------------------------------------------------------------------------------------------
  33. // THistorgramData: element in the histogram
  34. //----------------------------------------------------------------------------------------------
  35.  
  36. struct THistogramData {
  37. public:
  38.     THistogramData() {}
  39.     THistogramData(int nSequencePosition) { this->nSequencePosition = nSequencePosition; }
  40. public:
  41.     int    nSequencePosition;
  42. };
  43.  
  44. //----------------------------------------------------------------------------------------------
  45. // TSequenceData: element in the sequence
  46. //----------------------------------------------------------------------------------------------
  47.  
  48. struct TSequenceData {
  49. public:
  50.     TSequenceData() {}
  51.     TSequenceData(int Element) { this->Element = Element; }
  52. public:
  53.     int nMatchSize;
  54.     int    Element;
  55. };
  56.  
  57. //----------------------------------------------------------------------------------------------
  58. // CStringMatchPredictor: efficient O^N string-matching predictor
  59. //----------------------------------------------------------------------------------------------
  60.  
  61. class CStringMatchPredictor : public CPredictor {
  62. public:
  63.     // constructors & destructors
  64.  
  65.     CStringMatchPredictor() { m_nMaxSize = 0; m_nPrediction = 0; m_nSequenceLength = 0; m_nMinPerformance = 1; }
  66.     ~CStringMatchPredictor() {}
  67.  
  68. public:
  69.     // override members from predictor interface
  70.     virtual void    Update(int NextElement);
  71.     virtual bool    GetPrediction(int &Prediction);
  72.     virtual void    Reset();
  73.  
  74. public:
  75.     // predictor setup method
  76.     void            Setup(int nWindowSize, int nAlphabetSize, int nMinPerformance);
  77.  
  78. protected:
  79.     BOOL            HasNeighbour(int iWindowPosition, int Neighbour);
  80.     int                GetNeighbourSize(int iWindowPosition);
  81.  
  82. protected:
  83.     SMP_HISTOGRAM    m_Histogram;
  84.     SMP_WINDOW        m_Window;
  85.     int                m_PrevElement;
  86.     int                m_nWindowSize;
  87.     int                m_nSequenceLength;
  88.     int                m_nMaxSize;
  89.     int                m_nMaxPosition;
  90.     int                m_nPrediction;
  91.     int                m_nMinPerformance;
  92.     int                m_nAlphabetSize;
  93. };
  94.  
  95. //----------------------------------------------------------------------------------------------
  96. #endif // __STRINGMATCHPREDICTOR_H
  97.